﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NRadioTray.App.Domain;

using log4net;

namespace NRadioTray.App.Options
{
    public class OptionsPresenter : IDisposable, IOptionViewObserver
    {
        private Playlist playlist;
        private IOptionsView view;
        private readonly ILog logger;

        public OptionsPresenter(IOptionsView view, ILog logger)
        {
            this.view = view;
            this.logger = logger;
            this.view.SetObserver(this);
            this.playlist = Playlist.LoadPlaylist();
        }

        public void Show()
        {
            this.view.ShowRadios(playlist.Radios);
            this.view.ShowDialog();
        }

        public void Dispose()
        {
            this.view.Dispose();
        }

        public void MoveUp(Radio radio)
        {
            if (this.playlist == null)
            {
                return;
            }

            int index = this.playlist.Radios.IndexOf(radio);

            if (index > 0)
            {
                var tmp = this.playlist.Radios[index - 1].Copy();

                this.playlist.Radios[index - 1] = radio;
                this.playlist.Radios[index] = tmp;
                this.playlist.SavePlaylist();
                this.view.ShowRadios(playlist.Radios);
            }
        }

        public void MoveDown(Radio radio)
        {
            if (this.playlist == null)
            {
                return;
            }

            int index = this.playlist.Radios.IndexOf(radio);

            if(index < this.playlist.Radios.Count - 1)
            {
                var tmp = this.playlist.Radios[index + 1].Copy();

                this.playlist.Radios[index + 1] = radio;
                this.playlist.Radios[index] = tmp;
                this.playlist.SavePlaylist();
                this.view.ShowRadios(playlist.Radios);
            }

        }

        public void AddRadio()
        {
            using (var itemView = new ItemView())
            {
                var result = itemView.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }

                if (this.playlist != null)
                {
                    this.playlist.Radios.Add(itemView.Radio);
                    this.playlist.SavePlaylist();
                    this.view.ShowRadios(playlist.Radios);
                }
            }            
        }

        public void EditRadio(Radio radio)
        {
            using (var itemView = new ItemView())
            {
                itemView.Radio = radio;
                var result = itemView.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }

                if (this.playlist != null)
                {
                    int index = this.playlist.Radios.IndexOf(itemView.Radio);
                    this.playlist.Radios.RemoveAt(index);
                    this.playlist.Radios.Insert(index, itemView.Radio);
                    this.playlist.SavePlaylist();
                    this.view.ShowRadios(playlist.Radios);
                }
            }            
        }

        public void RemoveRadio(Radio radio)
        {
            if (this.playlist != null)
            {
                this.playlist.Radios.Remove(radio);
                this.playlist.SavePlaylist();
                this.view.ShowRadios(playlist.Radios);
            }            
        }
    }
}
